package com.situ.mall.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.situ.mall.pojo.User;
import com.situ.mall.service.IUserService;
import com.situ.mall.service.impl.UserServiceImpl;
import com.situ.mall.utils.JDBCUtil;
import com.situ.mall.utils.JSONUtil;
import com.situ.mall.vo.DeptCountVO;
import com.situ.mall.vo.UserDeptVO;

// http://localhost:8080/Mall/user
@WebServlet("/user")
public class UserServlet extends HttpServlet{
	private IUserService userService = new UserServiceImpl();
	
	// http://localhost:8080/Mall/user?method=selectAll
	// http://localhost:8080/Mall/user?method=deleteById&id=1
	// 访问servlet的时候默认访问service方法
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		switch (method) {
		case "selectAll":
			selectAll(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "add":
			add(req, resp);
			break;
		case "selectDeptCount":
			selectDeptCount(req, resp);
			break;
		case "login":
			login(req, resp);
			break;
		case "logout":
			logout(req, resp);
			break;
		case "selectById":
			selectById(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		default:
			break;
		}
	}

	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("UserServlet.update()");
		String id = req.getParameter("id");
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String email = req.getParameter("email");
		String role = req.getParameter("role");
		String deptId = req.getParameter("deptId");
		
		User user = new User(Integer.parseInt(id), Integer.parseInt(deptId), username, password, email, Integer.parseInt(role));
		userService.update(user);
		
		resp.sendRedirect(req.getContextPath() + "/user?method=selectAll");
	}

	private void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("UserServlet.selectById()");
		String id = req.getParameter("id");
		User user = userService.selectById(Integer.parseInt(id));
		req.setAttribute("user", user);
		req.getRequestDispatcher("/user_update.jsp").forward(req, resp);
	}

	private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("UserServlet.logout()");
		HttpSession session = req.getSession();
		session.invalidate();
		resp.sendRedirect(req.getContextPath() + "/login.jsp");
	}

	private void login(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("UserServlet.login()");
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		
		User user = userService.login(username, password);
		
		if (user == null) {
			//这个用户不存在
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("isOk", false);
			JSONUtil.obj2Json(map, resp);
			
		} else {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("isOk", true);
			JSONUtil.obj2Json(map, resp);
			HttpSession session = req.getSession();
			session.setAttribute("user", user);
		}
	}

	private void selectDeptCount(HttpServletRequest req, HttpServletResponse resp) {
		List<DeptCountVO> list = userService.selectDeptCount();
		JSONUtil.array2Json(list, resp);
	}

	private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("UserServlet.add()");
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String email = req.getParameter("email");
		String role = req.getParameter("role");
		String deptId = req.getParameter("deptId");
		
		User user = new User(Integer.parseInt(deptId), username, password, email, Integer.parseInt(role));
		userService.add(user);
		
		// 删除完数据之后，应该重定向到查找所有的界面
		resp.sendRedirect(req.getContextPath() + "/user?method=selectAll");
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("UserServlet.deleteById()");
		String id = req.getParameter("id");
		
		userService.deleteById(Integer.parseInt(id));
		// 删除完数据之后，应该重定向到查找所有的界面
		resp.sendRedirect(req.getContextPath() + "/user?method=selectAll");
	}
	

	private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("UserServlet.selectAll()");
		List<UserDeptVO> list =  userService.selectAll();
		for (UserDeptVO userDeptVO : list) {
			System.out.println(userDeptVO);
		}
		
		// 把servlet查找的数据放到req这个内存里面（request是一个域对象，可以把数据放到里面 HttpSession）
		req.setAttribute("list", list);
		// 查找出数据之后，转发jsp页面进行展示
		req.getRequestDispatcher("/user_list.jsp").forward(req, resp);
	}

}
